<?php

class UsersController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $tbl = new Application_Model_Users;
		$users = $tbl->listAll();
		if($users === null){
			echo "Записей еще нет.";
		}
		//Zend_Debug::dump($users);
		$this->view->users = $users;
    }

    public function addAction()
    {
        $addForm = new Application_Form_Adduser;
		if ($this->getRequest()->isPost()) {
			if ($addForm->isValid($this->getRequest()->getPost())) {
					$users = new Application_Model_Users;
					$newUser = $users->constrUser($addForm->getValues());
					if($newUser === null){
						echo "Ошибка создания пользователя";
						return;
					}
					$users->save($newUser);
					$this->_redirect('/users');
			}
		}
		$this->view->addForm = $addForm;
    }

    public function userAction()
    {
        $id = $this->getRequest()->getParam('ID');
		if($id == null){
			$this->_redirect('/users');
		}
		if(!Zend_Validate::is($id, "Digits")){
			$this->_redirect('/users');
		}
		$users = new Application_Model_Users;
		$user = $users->findById($id);
		if($user === null) {
			echo "Нет такого пользователя";
		}
		$this->view->user = $user;
    }


}





